﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ZMenFM.Network.Spider
{
    public enum ZMSpiderCodec
    {
        VJCD_UTF8 = 65001,
        VJCD_GB2312 = 0
    };

    public enum ZMSpiderCookie
    {
        VJUCT_NO_COOKIE,
        VJUCT_USE_COOKIE,
        VJUCT_SAVE_COOKIE
    };

    [DebuggerStepThrough]
    public class ZMSpider
    {
        private int idx;

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool vj_init_global_envi();

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void vj_release_global_envi();

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int vj_create_spider();

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void vj_release_spider(int idx);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool vj_init_context(int idx, bool auto_del_cookie, StringBuilder cookie_file, bool use_cookie);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern String vj_get_cookie_filename(int idx);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void vj_del_cookie_file(int idx);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool vj_push_get_data(int idx, StringBuilder name, StringBuilder value);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool vj_push_post_data(int idx, StringBuilder name, StringBuilder value);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void vj_clear_get_data(int idx);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void vj_clear_post_data(int idx);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr vj_get(int idx, StringBuilder url, uint code, int cookie);

        [DllImport("vjspider.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr vj_post(int idx, StringBuilder url, uint code, int cookie);

        [DebuggerStepThrough]
        public static bool Initialize()
        {
            return vj_init_global_envi();
        }

        [DebuggerStepThrough]
        public static void Release()
        {
            vj_release_global_envi();
        }

        [DebuggerStepThrough]
        public ZMSpider()
        {
            idx = vj_create_spider();
        }

        [DebuggerStepThrough]
        public bool InitContext(bool auto_del_cookie = true, String cookie_file = "$", bool use_cookie = true)
        {
            StringBuilder sbcookiefile = new StringBuilder(cookie_file);
            return vj_init_context(idx, auto_del_cookie, sbcookiefile, use_cookie);
        }

        [DebuggerStepThrough]
        public String GetCookiesFilename()
        {
            return vj_get_cookie_filename(idx);
        }

        [DebuggerStepThrough]
        public void DelCookiesFilename()
        {
            vj_del_cookie_file(idx);
        }

        [DebuggerStepThrough]
        public bool PushGetData(String name, String value)
        {
            StringBuilder sbName = new StringBuilder(name);
            StringBuilder sbValue = new StringBuilder(value);
            return vj_push_get_data(idx, sbName, sbValue);
        }

        [DebuggerStepThrough]
        public bool PushPostData(String name, String value)
        {
            StringBuilder sbName = new StringBuilder(name);
            StringBuilder sbValue = new StringBuilder(value);
            return vj_push_post_data(idx, sbName, sbValue);
        }

        [DebuggerStepThrough]
        public void ClearGetData()
        {
            vj_clear_get_data(idx);
        }

        [DebuggerStepThrough]
        public void ClearPostData()
        {
            vj_clear_post_data(idx);
        }

        [DebuggerStepThrough]
        public string Get(String url, ZMSpiderCodec code = ZMSpiderCodec.VJCD_UTF8, ZMSpiderCookie cookie = ZMSpiderCookie.VJUCT_USE_COOKIE)
        {
            StringBuilder sbUrl = new StringBuilder(url);
            IntPtr res = vj_get(idx, sbUrl, (uint)code, (int)cookie);
            string ans = Marshal.PtrToStringAnsi(res);
            ans = ans.Substring(0, ans.Length - 1);
            return ans;
        }

        [DebuggerStepThrough]
        public string Post(String url, ZMSpiderCodec code = ZMSpiderCodec.VJCD_UTF8, ZMSpiderCookie cookie = ZMSpiderCookie.VJUCT_SAVE_COOKIE)
        {
            StringBuilder sbUrl = new StringBuilder(url);
            IntPtr res = vj_post(idx, sbUrl, (uint)code, (int)cookie);
            string ans = Marshal.PtrToStringAnsi(res);
            ans = ans.Substring(0, ans.Length - 1);
            return ans;
        }

        [DebuggerStepThrough]
        ~ZMSpider()
        {
            vj_release_spider(idx);
        }
    }
}
